# -*- coding: utf-8 -*-
import pandas as pd

from CODEQL import CODEQL
from INFER import INFER
from SEMGREP import SEMGREP
from SPOTBUGS import SPOTBUGS

class MARK:
    '''对各项指标进行打分'''
    def __init__(self) -> None:
        self.codeql=CODEQL()
        self.infer=INFER()
        self.semgrep=SEMGREP()
        self.spotbugs=SPOTBUGS()
        self.result={
            '指标名称':['准确性指标','准确性指标','性能指标','性能指标','性能指标','性能指标','性能指标'],
            '评分项目':['RCR/召回率/准确率(0.5)','FPR/误报率(0.5)','时间平均检出率(0.4)','CPU占用率(0.2)','内存占用率(0.2)','磁盘读占用(0.1)','磁盘写占用(0.1)'],
            '评分原则':['表现最佳的项目为100分，其他项目与最佳数据为基准进行打分'] * 7,
            'CodeQL':[],
            'Infer':[],
            'Semgrep':[],
            'Spotbugs':[]
        }
        self.mark_accuracy()
        self.mark_performance()
        self.generate_result()

    def mark_accuracy(self):
        '''准确性指标打分'''
        codeql_pd=pd.read_excel('./analyze_result/codeql_analyze_result.xlsx',sheet_name='main')
        infer_pd=pd.read_excel('./analyze_result/infer_analyze_result.xlsx',sheet_name='main')
        semgrep_pd=pd.read_excel('./analyze_result/semgrep_analyze_result.xlsx',sheet_name='main')
        spotbugs_pd=pd.read_excel('./analyze_result/spotbugs_analyze_result.xlsx',sheet_name='main')        
        codeql_rcr=codeql_pd['rcr'][0]
        codeql_fpr=codeql_pd['fpr'][0]
        infer_rcr=infer_pd['rcr'][0]
        infer_fpr=infer_pd['fpr'][0]
        semgrep_rcr=semgrep_pd['rcr'][0]
        semgrep_fpr=semgrep_pd['fpr'][0]
        spotbugs_rcr=spotbugs_pd['rcr'][0]
        spotbugs_fpr=spotbugs_pd['fpr'][0]
        # RCR评分
        rcr_max=max(codeql_rcr,infer_rcr,semgrep_rcr,spotbugs_rcr)
        codeql_rcr_score=codeql_rcr/rcr_max*100
        infer_rcr_score=infer_rcr/rcr_max*100
        semgrep_rcr_score=semgrep_rcr/rcr_max*100
        spotbugs_rcr_score=spotbugs_rcr/rcr_max*100
        # FRP评分
        fpr_min=min(codeql_fpr,infer_fpr,semgrep_fpr,spotbugs_fpr)
        codeql_fpr_score=(1-codeql_fpr)/(1-fpr_min)*100
        infer_fpr_score=(1-infer_fpr)/(1-fpr_min)*100
        semgrep_fpr_score=(1-semgrep_fpr)/(1-fpr_min)*100
        spotbugs_fpr_score=(1-spotbugs_fpr)/(1-fpr_min)*100

        self.result['CodeQL'].append(codeql_rcr_score)
        self.result['CodeQL'].append(codeql_fpr_score)
        self.result['Infer'].append(infer_rcr_score)
        self.result['Infer'].append(infer_fpr_score)
        self.result['Semgrep'].append(semgrep_rcr_score)
        self.result['Semgrep'].append(semgrep_fpr_score)
        self.result['Spotbugs'].append(spotbugs_rcr_score)
        self.result['Spotbugs'].append(spotbugs_fpr_score)

    def mark_performance(self):
        '''性能指标打分'''
        def translate_rate_to_float(rate):
            '''把360.4KB/s转换成360400'''
            if 'GB/s' in rate:
                return float(rate[:-4])*1000*1000*1000
            if 'MB/s' in rate:
                return float(rate[:-4])*1000*1000
            if 'KB/s' in rate:
                return float(rate[:-4])*1000
            if 'B/s' in rate:
                return float(rate[:-3])
        
        codeql_pd=pd.read_excel('./analyze_result/codeql_performance.xlsx',sheet_name='result')
        infer_pd=pd.read_excel('./analyze_result/infer_performance.xlsx',sheet_name='result')
        semgrep_pd=pd.read_excel('./analyze_result/semgrep_performance.xlsx',sheet_name='result')
        spotbugs_pd=pd.read_excel('./analyze_result/spotbugs_performance.xlsx',sheet_name='result')

        print()

        codeql_cpu=float(codeql_pd['cpu'][0][:-1])
        codeql_mem=float(codeql_pd['mem'][0][:-1])
        codeql_read=translate_rate_to_float(codeql_pd['read'][0])
        codeql_write=translate_rate_to_float(codeql_pd['write'][0])
        codeql_time=codeql_pd['time'][0]

        infer_cpu=float(infer_pd['cpu'][0][:-1])
        infer_mem=float(infer_pd['mem'][0][:-1])
        infer_read=translate_rate_to_float(infer_pd['read'][0])
        infer_write=translate_rate_to_float(infer_pd['write'][0])
        infer_time=infer_pd['time'][0]

        semgrep_cpu=float(semgrep_pd['cpu'][0][:-1])
        semgrep_mem=float(semgrep_pd['mem'][0][:-1])
        semgrep_read=translate_rate_to_float(semgrep_pd['read'][0])
        semgrep_write=translate_rate_to_float(semgrep_pd['write'][0])
        semgrep_time=semgrep_pd['time'][0]
        
        spotbugs_cpu=float(spotbugs_pd['cpu'][0][:-1])
        spotbugs_mem=float(spotbugs_pd['mem'][0][:-1])
        spotbugs_read=translate_rate_to_float(spotbugs_pd['read'][0])
        spotbugs_write=translate_rate_to_float(spotbugs_pd['write'][0])
        spotbugs_time=spotbugs_pd['time'][0]

        codeql_pd=pd.read_excel('./analyze_result/codeql_analyze_result.xlsx',sheet_name='main')
        infer_pd=pd.read_excel('./analyze_result/infer_analyze_result.xlsx',sheet_name='main')
        semgrep_pd=pd.read_excel('./analyze_result/semgrep_analyze_result.xlsx',sheet_name='main')
        spotbugs_pd=pd.read_excel('./analyze_result/spotbugs_analyze_result.xlsx',sheet_name='main')
        codeql_success_report_number=codeql_pd['success_report_number'][0]
        infer_success_report_number=infer_pd['success_report_number'][0]
        semgrep_success_report_number=semgrep_pd['success_report_number'][0]
        spotbugs_success_report_number=spotbugs_pd['success_report_number'][0]
        # 时间平均检出率评分
        codeql_check_rate=codeql_success_report_number/codeql_time
        infer_check_rate=infer_success_report_number/infer_time
        semgrep_check_rate=semgrep_success_report_number/semgrep_time
        spotbugs_check_rate=spotbugs_success_report_number/spotbugs_time
        check_rate_max=max(codeql_check_rate,infer_check_rate,semgrep_check_rate,spotbugs_check_rate)
        codeql_check_rate_score=codeql_check_rate/check_rate_max*100
        infer_check_rate_score=infer_check_rate/check_rate_max*100
        semgrep_check_rate_score=semgrep_check_rate/check_rate_max*100
        spotbugs_check_rate_score=spotbugs_check_rate/check_rate_max*100

        # CPU占用率评分
        cpu_min=min(codeql_cpu,infer_cpu,semgrep_cpu,spotbugs_cpu)
        codeql_cpu_score=cpu_min/codeql_cpu*100
        infer_cpu_score=cpu_min/infer_cpu*100
        semgrep_cpu_score=cpu_min/semgrep_cpu*100
        spotbugs_cpu_score=cpu_min/spotbugs_cpu*100
        # 内存占用率评分
        mem_min=min(codeql_mem,infer_mem,semgrep_mem,spotbugs_mem)
        codeql_mem_score=mem_min/codeql_mem*100
        infer_mem_score=mem_min/infer_mem*100
        semgrep_mem_score=mem_min/semgrep_mem*100
        spotbugs_mem_score=mem_min/spotbugs_mem*100
        # 磁盘IO评分
        read_min=min(codeql_read,infer_read,semgrep_read,spotbugs_read)
        codeql_read_score=read_min/codeql_read*100
        infer_read_score=read_min/infer_read*100
        semgrep_read_score=read_min/semgrep_read*100
        spotbugs_read_score=read_min/spotbugs_read*100
        write_min=min(codeql_write,infer_write,semgrep_write,spotbugs_write)
        codeql_write_score=write_min/codeql_write*100
        infer_write_score=write_min/infer_write*100
        semgrep_write_score=write_min/semgrep_write*100
        spotbugs_write_score=write_min/spotbugs_write*100

        self.result['CodeQL'].append(codeql_check_rate_score)
        self.result['CodeQL'].append(codeql_cpu_score)
        self.result['CodeQL'].append(codeql_mem_score)
        self.result['CodeQL'].append(codeql_read_score)
        self.result['CodeQL'].append(codeql_write_score)
        self.result['Infer'].append(infer_check_rate_score)
        self.result['Infer'].append(infer_cpu_score)
        self.result['Infer'].append(infer_mem_score)
        self.result['Infer'].append(infer_read_score)
        self.result['Infer'].append(infer_write_score)
        self.result['Semgrep'].append(semgrep_check_rate_score)
        self.result['Semgrep'].append(semgrep_cpu_score)
        self.result['Semgrep'].append(semgrep_mem_score)
        self.result['Semgrep'].append(semgrep_read_score)
        self.result['Semgrep'].append(semgrep_write_score)
        self.result['Spotbugs'].append(spotbugs_check_rate_score)
        self.result['Spotbugs'].append(spotbugs_cpu_score)
        self.result['Spotbugs'].append(spotbugs_mem_score)
        self.result['Spotbugs'].append(spotbugs_read_score)
        self.result['Spotbugs'].append(spotbugs_write_score)

    def generate_result(self):
        '''生成最终评分excel文件'''
        df=pd.DataFrame(self.result)
        # 单元格合并
        df.set_index('评分原则')
        df.to_excel('./analyze_result/sast_total_result.xlsx',sheet_name='Sheet1',merge_cells=True,)

def test():
    # codeql=CODEQL()
    # semgrep=SEMGREP()
    # infer=INFER()
    # spotbugs=SPOTBUGS()
    mark=MARK()
    pass

test()
pass